package com.coszero.utils.device

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import java.lang.reflect.Method

/**
 * Desc： 虚拟键工具类
 *
 * @author xmqian
 * Email:xmqian93@163.com
 * Date: 2020/9/16 0016 11:07
 * @version 1
 */
class NavigationBarUtils {
    /**
     * @param context context
     * @return 返回是否有虚拟按键
     */
    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar: Boolean = false
        val rs: Resources = context.getResources()
        val id: Int = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass: Class<*> = Class.forName("android.os.SystemProperties")
            val m: Method = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride: String? =
                m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String?
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }
        return hasNavigationBar
    }

    companion object {
        /**
         * @param activity 隐藏底部虚拟键
         */
        fun hintNavigation(activity: Activity) {
            //隐藏虚拟键
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                val attributes: WindowManager.LayoutParams = activity.getWindow().getAttributes()
                attributes.systemUiVisibility =
                    (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
                activity.getWindow().setAttributes(attributes)
            }
        }

        /**
         * 弹窗使用
         *
         * @param window window 这个需要从弹窗中获取
         * @param context context
         */
        @JvmStatic
        fun hideNavigationBar(window: Window, context: Context) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return
            val decorView: ViewGroup = window.getDecorView() as ViewGroup
            var i: Int = 0
            val count: Int = decorView.getChildCount()
            while (i < count) {
                val child: View = decorView.getChildAt(i)
                val id: Int = child.getId()
                if (id != View.NO_ID) {
                    val resourceEntryName: String = getResNameById(context, id)
                    if ("navigationBarBackground" == resourceEntryName) {
                        child.setVisibility(View.INVISIBLE)
                    }
                }
                i++
            }

            /* window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);*/
            val uiOptions: Int = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //                View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_IMMERSIVE or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() or uiOptions)
        }

        private fun getResNameById(context: Context, id: Int): String {
            try {
                return context.getResources().getResourceEntryName(id)
            } catch (ignore: Exception) {
                return ""
            }
        }
    }
}
